// pages/checkpapers/checkpapers.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wenstate:false,
    status: false,
    url:'',
    gettestpaperurl: app.data.url + '/zhongxue/maoapi/gettest_paper',
    dowfiles: app.data.url + '/zhongxue/maoapi/dowfiles',
    xiala: app.data.url+'/uploads/zhongxue/xiala.png',
    wenjian: app.data.url +'/uploads/zhongxue/wenjian.png',
    arrayduan: ["小学", "初中", "高中", "大学"],
    arraynian: ["一年级", "二年级", "三年级", "四年级"],
    arrayke: ["语文", "数学", "英语", "化学"],
    arras: [
      {
        images: [app.data.url + "/uploads/zhongxue/shijuans.png", app.data.url + "/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], xiaoqu: "成都校区", length: "2", title: "四年级语文期中考试卷精选", date: "2018-16-17" },
      { images: [app.data.url + "/uploads/zhongxue/shijuans.png", app.data.url + "/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], xiaoqu: "成都校区", length: "2", title: "四年级语文期中考试卷精选", date: "2018-16-17" },
      { images: [app.data.url + "/uploads/zhongxue/shijuans.png", app.data.url + "/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], xiaoqu: "成都校区", length: "2", title: "四年级语文期中考试卷精选", date: "2018-16-17" },
    ],
    page: 1,
    tiaojian: {},
    shuju: [],
    section: [],
    grades: [],
    curriculum: [],
    xueduan: '学段',
    nianji: '年级',
    kecheng: '科目'
  },

  //点击图片放大
  imge: function (e) {
    var index = e.currentTarget.dataset.index;
    var path = this.data.shuju[index].shijuans;
    // var bool = ea.isImg(path);
    wx.previewImage({
      urls: path,
    })
  },
  wendang:function(e){
    console.log(e);
    // wx.showModal({
    //   title: '提示',
    //   content: '请在电脑端下载资料,官方网址：'+this.data.url,
    //   showCancel: false,
    //   // cancelText: '',
    //   // cancelColor: '',
    //   confirmText: '确定',
    //   // confirmColor: '',
    //   success: function(res) {},
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
    var index = e.currentTarget.dataset.index;
    var path = this.data.shuju[index].wendang;
    wx.showLoading({
      title: '加载中',
    })
    console.log(path);
    wx.downloadFile({
      url: path,
      header: {},
      success: function(res) {
        var tempFilePath = res.tempFilePath;
        wx.openDocument({
          filePath: tempFilePath,
          success: function () {
            wx.hideLoading();
          }
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })

  },
  xiazai:function(e){
    wx.showModal({
      title: '提示',
      content: '请在电脑端下载资料,官方网址：' + this.data.url,
      showCancel: false,
      // cancelText: '',
      // cancelColor: '',
      confirmText: '确定',
      // confirmColor: '',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    // var id = e.currentTarget.dataset.id;
    // var that = this;
    // wx.showLoading({
    //   title: '下载中',
    // })
    // wx.downloadFile({
    //   url: that.data.dowfiles+'?id='+id,
    //   header: {},
    //   success: function(res) {
    //     wx.hideLoading();
    //     var tempFilePath = res.tempFilePath;
    //     wx.saveFile({
    //       tempFilePath: tempFilePath,
    //       success: function(ress) {
    //         console.log(ress)
    //         var savedFilePath = ress.savedFilePath;
    //         wx.openDocument({
    //           filePath: savedFilePath,
    //           fileType: '',
    //           success: function(){
    //             wx.showToast({
    //               title: '已下载到' + savedFilePath,
    //               icon: 'none',
    //               image: '',
    //               duration: 5000,
    //               mask: true,
    //               success: function(res) {},
    //               fail: function(res) {},
    //               complete: function(res) {},
    //             })
    //           },
    //           fail: '',
    //           complete: '',
    //         })
    //       },
    //       fail: function(res) {},
    //       complete: function(res) {},
    //     })
    //   },
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getdata: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var page = this.data.page;
    if (page == 0) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: app.data.quanbushijian
      })
      return
    }
    var tiaojian = this.data.tiaojian;
    // console.log(tiaojian);
    tiaojian.token = wx.getStorageSync('user').token;
    tiaojian.page = page;
    // console.log(tiaojian)
    wx.request({
      url: that.data.gettestpaperurl,
      data: tiaojian,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data);
        if (data.code == 200) {
          var url = data.data.url;
          that.setData({url:url});
          var section = data.data.section;
          var grades = data.data.grades;
          var curriculum = data.data.curriculum;
          section.unshift({ name: '全学段' })
          grades.unshift({ name: '全年级' })
          curriculum.unshift({ name: '全科目' })
          that.setData({ section: section, grades: grades, curriculum: curriculum });
          var shuju = that.data.shuju;
          var newshuju = data.data.data;
          if (newshuju.length < 10) {
            that.setData({ page: 0 });
          } else {
            that.setData({ page: page + 1 })
          }
          ea.each(data.data.data, function (i, v) {
            if (ea.isImg(v.shijuans)){
              v.img = true;
            }else{
              v.img = false;
            }
            shuju.push(v);
          })
          console.log(shuju);
          that.setData({ shuju: shuju, status: true });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: app.data.quanbushijian
          })
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  changexueduan: function (e) {
    // console.log(e);
    var key = e.currentTarget.dataset.key;
    var index = e.detail.value;
    var shuju = this.data[key];
    var tiaojian = this.data.tiaojian;
    if (shuju[index].id) {
      tiaojian[key] = shuju[index].id;
    } else {
      tiaojian[key] = '';
    }
    this.setData({ tiaojian: tiaojian });
    if (key == 'section') {
      this.setData({ xueduan: shuju[index].name })
      tiaojian.grades = "";
      this.setData({ tiaojian: tiaojian });
      this.setData({ nianji:"全年级"});
      // this.setData({ nianji:})
    } else if (key == 'grades') {
      this.setData({ nianji: shuju[index].name })
    } else if (key == 'curriculum') {
      this.setData({ kecheng: shuju[index].name })
    }
    this.setData({ page: 1, shuju: [] });
    this.getdata();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      tiaojian: {},
      page: 1,
      shuju: [],
      xueduan: '学段',
      nianji: '年级',
      kecheng: '科目'
    });
    this.getdata();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getdata();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})